local Widget = require "widgets/widget"
local Image = require "widgets/image" -- 引入image控件
-- local UIAnim = require "widgets/uianim"


-- local Screen = require "widgets/screen"
-- local AnimButton = require "widgets/animbutton"
-- local ImageButton = require "widgets/imagebutton"
-- local Menu = require "widgets/menu"
-- local Text = require "widgets/text"
-- local TEMPLATES = require "widgets/redux/templates"
-- local Video = require "widgets/video"



local AnimButton = require "widgets/animbutton_npc_card"

local npc_cards_widget = Class(Widget, function(self, owner)
    Widget._ctor(self, "npc_cards_widget")
    self.owner = owner
end)


function npc_cards_widget:Create_Cards(ret_card_name)
    self.the_cards = {}
    
    for i = 1, 4, 1 do
        self.the_cards[i] = self:AddChild(AnimButton("npc_card_base",{
            scale_num = 1.2,
            scale_num_focus = 1.4,
            index = i,
        }))
        -- self.the_cards[i]:SetRotation(1.5)
    end

    self.the_cards[1]:SetPosition(-450,0)
    self.the_cards[2]:SetPosition(-150,0)
    self.the_cards[3]:SetPosition(150,0)
    self.the_cards[4]:SetPosition(450,0)

    for i, theCard in ipairs(self.the_cards) do
        theCard:SetOnClick(function()
            -- print("card click",self.owner)
            theCard:ClearFocus()
            for k, v in pairs(self.the_cards) do
                v:Disable() -- -- -- --无法点击，也不会触发 mouseover 事件
                v:SetOnClick(nil)   ---- 防止autoclick 再次执行
                -- v:SetRotation(0)
            end
            local tempInst = CreateEntity()
            tempInst._num = 1
            tempInst._down_flag = true
            local delta = 0.3
            tempInst.__task = tempInst:DoPeriodicTask(0.05,function()       ---- 卡牌翻转的 代码
                if tempInst._down_flag == true then
                    tempInst._num = tempInst._num - delta
                else
                    tempInst._num = tempInst._num + delta
                end
                theCard:SetScale(tempInst._num,theCard.scale_num_focus)
                if tempInst._num <= 0 then
                    tempInst._down_flag = false
                    ---- layer cover
                    theCard.anim:GetAnimState():OverrideSymbol("front","npc_cards",ret_card_name or "npc_card_test")
                    ---- layer cover
                elseif tempInst._num >= theCard.scale_num_focus then
                    theCard:SetScale(theCard.scale_num_focus,theCard.scale_num_focus)
                    tempInst.__task:Cancel()
                    tempInst:Remove()
                end
            end)

            if self.owner then
                self.owner:DoTaskInTime(4,function()
                    self.owner:PushEvent("npc_cards_sys.kill")
                    self.owner.replica.npc_base_lib:PushEvent("npc_cards_sys.clicked")
                end)
            end

        end)
    end

    if TUNING.NPC_CONFIG.CARD_AUTO_SELECT then
        local s_inst = CreateEntity()
        s_inst:DoTaskInTime(2.5,function()
            local ret_num = math.random(4)
            if self.the_cards[ret_num].onclick then
                self.the_cards[ret_num].onclick()
            end
            s_inst:Remove()
        end)
    elseif Input:ControllerConnected()  then    ---- 用手柄的话
        self.the_cards[1]:SetFocus()
        -- local ret_num = math.random(4)        
        -- -- self.the_cards[ret_num]:SetFocus()
        -- self.the_cards[ret_num].focus_flag = true
    end

end

-- function npc_cards_widget:OnControl(control, down)
--     print(control,down,"++++++++",math.random(5))
--     if control == nil or down == nil then
--         return
--     end
--     -- if npc_cards_widget._base.OnControl(self, control, down) then return true end

--     -- if not down then
--     --     if control == CONTROL_OPEN_DEBUG_CONSOLE then
--     --         return true
--     --     elseif control == CONTROL_CANCEL then
--     --         self:OnCancel()
--     --     end
--     -- end
--     return true
-- end

return npc_cards_widget